package edu.uwm.cs552;

import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import edu.uwm.cs552.util.Pair;

public class PlayersTrain {

	private HexCoordinate position;
	private List<HexCoordinate> movement;
	
	public PlayersTrain() {
		position = null;
		movement = new ArrayList<HexCoordinate>();
	}
	
	public HexCoordinate addMovement(HexCoordinate h) {
		if (movement.add(h))
			return h;
		return null;
	}
	
	public HexCoordinate remeoveMovement(HexCoordinate h) {
		if (movement.remove(h))
			return h;
		return null;
	}
	
	public HexCoordinate getNextMove() {
		if(movement.isEmpty()) return null;
		return movement.get(0);
	}
	
	public List<HexCoordinate> getMovements() {
		return movement;
	}
	
	public HexCoordinate getPosition() {
		return position;
	}
	
	public void setPosition(HexCoordinate h) {
		position = h;
	}
	
	public HexEdge getNextTrackUsed() {
		if(position == null) return null;
		Point now = position.toPoint(20.0);
		Point next = getNextMove().toPoint(20.0);
		return new HexEdge(position, HexDirection.fromDelta(next.getX() - now.getX(), next.getY() - now.getY()));
	}
	
	public void move() {
		if(!movement.isEmpty()) {
			position = movement.get(0);
			movement.remove(0);
		}
	}
	
	public double calculateCharge(RailGame game, Player p) {
		HexEdge edge = getNextTrackUsed();
		double charge = 0;
		
		if(game.isAnotherPlayers(p, edge)) {
			charge += 0.5;
			if(game.getBoard().getBarrier(edge) == Barrier.INLET)
				charge += 0.5;
		}
		return charge;
	}
	
	public void draw(Graphics g, double scale) {
		if(position == null) return;
		Point mid = position.toPoint(scale);
		Point l1p1 = new Point((int)Math.round(mid.getX()-(0.5*scale)),(int)Math.round(mid.getY()+(0.5*scale)));
		Point l1p2 = new Point((int)Math.round(mid.getX()+(0.5*scale)),(int)Math.round(mid.getY()-(0.5*scale)));
		Point l2p1 = new Point((int)Math.round(mid.getX()+(0.5*scale)),(int)Math.round(mid.getY()+(0.5*scale)));
		Point l2p2 = new Point((int)Math.round(mid.getX()-(0.5*scale)),(int)Math.round(mid.getY()-(0.5*scale)));
		
		g.drawLine(l1p1.x, l1p1.y, l1p2.x, l1p2.y);
		g.drawLine(l2p1.x, l2p1.y, l2p2.x, l2p2.y);
	}
}
